/**
 * Copyright 2010 Andreas Keefer
 *
 * This file is part of jHashMe.
 *
 * jHashMe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jHashMe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with jHashMe.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.jhashme.ui.cli;

import org.apache.commons.cli.*;

/**
 * jHashMe command line client
 * <p/>
 * parameters:
 * <p/>
 * -h -help                                 Show Help
 * -l -list-algorithms                      List all available hash algorithms
 * -a -hash-algorithm   <alg>               Specify the hash algorithm
 * -f -file             <file>              The file to hash
 * -t -text             <text>              The text to hash
 * -
 * -p -checksum-pattern <pattern|template>
 */
public class JHashMeCli {

    public static void main(String[] args) {
        //args = new String[]{"-a", "lala"};

        Option help = new Option("h", "help", false, "print this message");
        Option listAlgorithms = new Option("l", "list-algorithms", false, "list the availabe hash algorithms and exit");
        Option createFile = new Option("c", "create-hashfile", false, "create a checksum file");
        Option checkFile = OptionBuilder.create("");
        Option checksumPattern = OptionBuilder
                .withLongOpt("pattern")
                .withDescription("specifies the pattern to use as a pattern suitable for java.txt.MessageFormat where " +
                        "{0} is replaced with the checksum, " +
                        "{1} with the file name and " +
                        "{2} with the hash algorithm")
                .hasArg()
                .withArgName("pattern")
                .create('p');
        Option algorithm = OptionBuilder
                .withLongOpt("algorithm")
                .withDescription("use given hash algorithm")
                .hasArg()
                .withArgName("algorithm")
                .create('a');
        Option hash = OptionBuilder
                .withLongOpt("hash")
                .hasArg()
                .withArgName("file or text")
                .withDescription("calculate hash from file or text")
                .create();

        // This group of options are mutually exclusive.
        OptionGroup exclusiveOptions = new OptionGroup()
                .addOption(listAlgorithms)
                .addOption(hash);
        exclusiveOptions.setRequired(true);

        Options options = new Options()
                .addOption(help)
                .addOption(algorithm)
                .addOption(checksumPattern)
                .addOptionGroup(exclusiveOptions);


        CommandLineParser parser = new PosixParser();
        try {
            // parse the command line arguments
            CommandLine line = parser.parse(options, args, true);

        }
        catch (ParseException exp) {
            // oops, something went wrong
            System.err.println("Parsing failed.  Reason: " + exp.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("jhashme", options, true);
        }

    }

//    private static void parseArgs(String[] argv) {
//        int parameter;
//        String arg;
//        final List<LongOpt> longopts = new ArrayList<LongOpt>();
//        //StringBuffer sb = new StringBuffer();
//
//        longopts.add(new LongOpt("list-algorithms", LongOpt.NO_ARGUMENT, null, 'l'));
//        longopts.add(new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'));
//        longopts.add(new LongOpt("hash-algorithm", LongOpt.REQUIRED_ARGUMENT, null, 'a'));
//        longopts.add(new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f'));
//        longopts.add(new LongOpt("text", LongOpt.REQUIRED_ARGUMENT, null, 't'));
//
//        Getopt g = new Getopt("jHashMe", argv, "lha::f::t::", longopts.toArray(new LongOpt[longopts.size()]));
//        //g.setOpterr(false); // We'll do our own error handling
//        //
//        while ((parameter = g.getopt()) != -1)
//            switch (parameter) {
////                case 0:
////                    arg = g.getOptarg();
////                    System.out.println("Got long option with value '" +
////                            (char) (new Integer(sb.toString())).intValue()
////                            + "' with argument " +
////                            ((arg != null) ? arg : "null"));
////                    break;
////                //
////                case 1:
////                    System.out.println("I see you have return in order set and that " +
////                            "a non-option argv element was just found " +
////                            "with the value '" + g.getOptarg() + "'");
////                    break;
////                //
////                case 2:
////                    arg = g.getOptarg();
////                    System.out.println("I know this, but pretend I didn't");
////                    System.out.println("We picked option " +
////                            longopts[g.getLongind()].getName() +
////                            " with value " +
////                            ((arg != null) ? arg : "null"));
////                    break;
////                //
//                case 'l':
//                    print(hashService.getAvailableHashAlgorithms());
//                    break;
//                //
//                case 'c':
//                case 'd':
//                    arg = g.getOptarg();
//                    System.out.println("You picked option '" + (char) parameter +
//                            "' with argument " +
//                            ((arg != null) ? arg : "null"));
//                    break;
//                //
//                case 'h':
//                    System.out.println("I see you asked for help");
//                    break;
//                //
//                case 'W':
//                    System.out.println("Hmmm. You tried a -W with an incorrect long " +
//                            "option name");
//                    break;
//                //
//                case ':':
//                    System.out.println("Doh! You need an argument for option " +
//                            (char) g.getOptopt());
//                    break;
//                //
//                case '?':
//                    System.out.println("The option '" + (char) g.getOptopt() +
//                            "' is not valid");
//                    break;
//                //
//                default:
//                    System.out.println("getopt() returned " + parameter);
//                    break;
//            }
//        //
//        for (int i = g.getOptind(); i < argv.length; i++)
//            System.out.println("Non option argv element: " + argv[i] + "\n");
//    }
}
